package org.hdax.dm.rest.service;

import org.hdax.dm.dao.entitydao.DmSchedulerSeatDao;
import org.hdax.dm.entity.scheduler_seat.DmSchedulerSeat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@RestController
public class RestSchedulerSeatService {

    @Autowired
    private DmSchedulerSeatDao dmSchedulerSeatDao;

    @PostMapping(path = "/findByDmSchedulerSeatCount")
    public Long findByDmSchedulerSeatCount(@RequestBody DmSchedulerSeat dmSchedulerSeat) {
        return dmSchedulerSeatDao.count(where(dmSchedulerSeat));
    }

    @PostMapping(path = "/getSchedulerSeatByOrder")
    public DmSchedulerSeat getSchedulerSeatByOrder(@RequestParam("scheduleId") Integer scheduleId,
                                                   @RequestParam("x") Integer x,
                                                   @RequestParam("y") Integer y) {
        return dmSchedulerSeatDao.findOne(new Specification<DmSchedulerSeat>() {
            @Override
            public Predicate toPredicate(Root<DmSchedulerSeat> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                //具体的条件判断
                if (scheduleId != null) {
                    predicates.add(criteriaBuilder.equal(root.get("scheduleId").as(Long.class), scheduleId));
                }

                if (x != null) {
                    predicates.add(criteriaBuilder.equal(root.get("X").as(Integer.class), x));
                }

                if (y != null) {
                    predicates.add(criteriaBuilder.equal(root.get("Y").as(Integer.class), y));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
            }
        }).get();
    }

    @PostMapping(path = "/modifySchedulerSeat")
    public void modifySchedulerSeat(@RequestBody DmSchedulerSeat schedulerSeat) {
        dmSchedulerSeatDao.save(schedulerSeat);
    }

    public Specification<DmSchedulerSeat> where(DmSchedulerSeat dmSchedulerSeat) {
        return new Specification<DmSchedulerSeat>() {
            @Override
            public Predicate toPredicate(Root<DmSchedulerSeat> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (dmSchedulerSeat != null) {
                    if (dmSchedulerSeat.getScheduleId() != null && dmSchedulerSeat.getScheduleId() > 0) {
                        predicateList.add(criteriaBuilder.equal(root.get("scheduleId").as(Integer.class), dmSchedulerSeat.getScheduleId()));
                    }
                    if (dmSchedulerSeat.getStatus() != null && dmSchedulerSeat.getStatus() > 0) {
                        predicateList.add(criteriaBuilder.equal(root.get("status").as(Integer.class), dmSchedulerSeat.getStatus()));
                    }
                    if (dmSchedulerSeat.getAreaLevel() != null && dmSchedulerSeat.getAreaLevel() > 0) {
                        predicateList.add(criteriaBuilder.equal(root.get("areaLevel").as(Integer.class), dmSchedulerSeat.getAreaLevel()));
                    }
                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
            }
        };
    }
}
